package com.feishi.core.media.javacv;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-4-20 17:17
 **/



import com.feishi.core.media.javacv.util.JavaCvUtil;
import com.feishi.core.media.javacv.util.OpenCvUtil;
import com.feishi.core.util.common.ByteUtil;
import com.feishi.core.util.queue.Customer;
import com.feishi.core.util.queue.Producer;
import com.feishi.core.util.queue.feishi.FeishiProducer;
import com.feishi.core.util.queue.feishi.FeishiQueueUtil;
import com.feishi.core.util.queue.feishi.Msg;
import org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_videoio.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.nio.ByteBuffer;


public class CameraBasic {



    private JFrame frame;
    static JLabel label;
    static int flag = 0;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {


        EventQueue.invokeLater(CameraBasic::run);

        VideoCapture camera = new VideoCapture();
        camera.open(0);

        if (!camera.isOpened()) {
            System.out.println("Camera Error");
        } else {
            Mat frame = new Mat();

            long time =System.currentTimeMillis();
            long[] finalTime =new long[] {time};
            Producer<Msg<Integer, Mat>> productor = FeishiQueueUtil.getProductor((integerBufferedImageMsg, sequence, endOfBatch) -> {
                label.setIcon(new ImageIcon(OpenCvUtil.MatToBufImg(integerBufferedImageMsg.getBody())));
                System.out.println("intime:"+integerBufferedImageMsg);
            });
            int i=0;
            while (flag == 0) {
                camera.read(frame);

                System.out.println(System.currentTimeMillis()-time);
                time =System.currentTimeMillis();
                // 捕捉
//                BufferedImage img = matToBufferedImage(frame);
//                Mat mat=JavaCvUtil.bufImgToMat(img);
                productor.push(new Msg<Integer, Mat>(i++%4,frame){
                    private long time=System.currentTimeMillis();

                    @Override
                    public String toString() {
                        return (System.currentTimeMillis()-time)+"";
                    }
                });

                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Create the application.
     */
    public CameraBasic() {
        initialize();
    }

    private static void run() {
        try {
            CameraBasic window = new CameraBasic();
            window.frame.setVisible(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 800, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("\u62CD\u7167");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                flag = 1;
            }
        });
        btnNewButton.setBounds(33, 13, 113, 27);
        frame.getContentPane().add(btnNewButton);

        label = new JLabel("");
        label.setBounds(0, 0, 800, 450);
        frame.getContentPane().add(label);
    }

}
